"""
二叉树的构建与遍历
"""


class TreeNode:
    """
        二叉树节点
    """

    def __init__(self, data=None, left=None, right=None):
        self.data = data
        self.left = left
        self.right = right


class TwoTree:
    """
        二叉树
    """

    def __init__(self, root=None):
        self.root = root

    def is_empty(self):
        if self.root is None:
            return True
        else:
            return False

    def pre_order(self, node):
        """
            前序遍历
        :param node:
        :return:
        """
        if node is None:
            return
        print(node.data, end=' ')
        self.pre_order(node.left)
        self.pre_order(node.right)

    def in_order(self, node):
        """
            中序遍历
        :param node:
        :return:
        """
        if node is None:
            return
        self.in_order(node.left)
        print(node.data, end=' ')
        self.in_order(node.right)

    def post_order(self, node):
        """
            后序遍历
        :param node:
        :return:
        """
        if node is None:
            return
        self.post_order(node.left)
        self.post_order(node.right)
        print(node.data, end=' ')


if __name__ == "__main__":
    # 按照后序遍历增加节点
    b = TreeNode('B')
    f = TreeNode('F')
    g = TreeNode('G')
    d = TreeNode('D', f, g)
    i = TreeNode('I')
    h = TreeNode('H')
    e = TreeNode('E', i, h)
    c = TreeNode('C', d, e)
    a = TreeNode('A', b, c)

    # 初始化树对象，传入根节点
    bt = TwoTree(a)
    print("前序遍历")
    bt.pre_order(bt.root)
    print("\n中序遍历")
    bt.in_order(bt.root)
    print("\n后序遍历")
    bt.post_order(bt.root)

